package com.lizard.schedule.utils;

import android.content.res.Resources;

import com.lizard.schedule.ScheduleApplication;
import com.lizard.schedule.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间处理工具
 */
public class TimeUtil {

    /**
     * 一天的毫秒数
     */
    public static final long DAY_MILLIS = 24 * 60 * 60 * 1000;

    /**
     * 一小时的毫秒数
     */
    public static final long HOUR_MILLIS = 60 * 60 * 1000;

    /**
     * 一分钟的毫秒数
     */
    public static final long MINUTE_MILLIS = 60 * 1000;

    /**
     * 获取这个时间是今天、明天、后天、昨天、前天
     *
     * @param currentTimeMills
     * @param timeMills
     * @return 返回一下值之一："今天"、"明天"、"后天"、"昨天"、"前天"、""
     */
    public static String getDayNearby(long currentTimeMills, long timeMills){
        return getDayNearby(currentTimeMills, timeMills, false);
    }

    /**
     * 获取这个时间是今、明、后、昨、前
     *
     * @param currentTimeMills
     * @param timeMills
     * @return 返回一下值之一："今"、"明"、"后"、"昨"、"前"、""
     */
    public static String getBrefDayNearby(long currentTimeMills, long timeMills){
        return getDayNearby(currentTimeMills, timeMills, true);
    }

    /**
     *
     * @param currentTimeMills
     * @param timeMills
     * @param bref
     * @return
     */
    private static String getDayNearby(long currentTimeMills, long timeMills, boolean bref){
        long interval = getDayInterval(timeMills, currentTimeMills);
        String day = "";
        if(interval == 0){
            day = bref ? ScheduleApplication.applicationContext.getString(R.string.bref_today)
                    : ScheduleApplication.applicationContext.getString(R.string.today);
        } else if(interval == 1){
            day = bref ? ScheduleApplication.applicationContext.getString(R.string.bref_tomorrow)
                    : ScheduleApplication.applicationContext.getString(R.string.tomorrow);
        } else if(interval == 2){
            day = bref ? ScheduleApplication.applicationContext.getString(R.string.bref_day_after_tomorrow)
                    : ScheduleApplication.applicationContext.getString(R.string.day_after_tomorrow);
        } else if(interval == -1){
            day = bref ? ScheduleApplication.applicationContext.getString(R.string.bref_yesterday)
                    : ScheduleApplication.applicationContext.getString(R.string.yesterday);
        } else if(interval == -2){
            day = bref ? ScheduleApplication.applicationContext.getString(R.string.bref_day_before_yesterday)
                    : ScheduleApplication.applicationContext.getString(R.string.day_before_yesterday);
        }
        return day;
    }

    /**
     * 获取时间的间隔天数 time1-time2
     *
     * @param time1
     * @param time2
     * @return
     */
    public static long getDayInterval(long time1, long time2){
        Calendar ca1 = Calendar.getInstance();
        ca1.setTimeInMillis(time1);
        ca1.set(Calendar.HOUR_OF_DAY, 0);
        ca1.set(Calendar.MINUTE, 0);
        ca1.set(Calendar.SECOND, 0);

        Calendar ca2 = Calendar.getInstance();
        ca2.setTimeInMillis(time2);
        ca2.set(Calendar.HOUR_OF_DAY, 0);
        ca2.set(Calendar.MINUTE, 0);
        ca2.set(Calendar.SECOND, 0);

        long dif = ca1.getTimeInMillis() - ca2.getTimeInMillis();

        return Math.round(dif * 1.0 / DAY_MILLIS );
    }

    /**
     * 日期时间字符串转换到毫秒
     *
     * @param time 格式：yyyy-M-d HH:mm
     * @return
     */
    public static long transferToTimeMills(String time){
        long timeMillis = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm");
            Date date = simpleDateFormat.parse(time);
            timeMillis = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeMillis;
    }

    /**
     * 日期时间字符串转换到毫秒
     *
     * @param time 格式：yyyy-MM-dd HH:mm
     * @return
     */
    public static long transferToTimeMills2(String time){
        long timeMillis = 0;
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = simpleDateFormat.parse(time);
            timeMillis = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeMillis;
    }

    /**
     * 毫秒转换到日期加时间字符串
     *
     * @param timeMills
     * @return 格式：yyyy-M-d HH:mm
     */
    public static String transferToDateTime(long timeMills){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm");
        return simpleDateFormat.format(new Date(timeMills));
    }

    /**
     * 毫秒转换到日期加时间字符串
     *
     * @param timeMills
     * @return 格式：yyyy年M月d日 HH时mm分
     */
    public static String transferToDateTime2(long timeMills){
        StringBuilder timeBuilder = new StringBuilder();
        timeBuilder.append("yyyy")
                .append(ScheduleApplication.applicationContext.getString(R.string.year))
                .append("M")
                .append(ScheduleApplication.applicationContext.getString(R.string.month))
                .append("d")
                .append(ScheduleApplication.applicationContext.getString(R.string.day))
                .append(" HH")
                .append(ScheduleApplication.applicationContext.getString(R.string.hour))
                .append("mm")
                .append(ScheduleApplication.applicationContext.getString(R.string.minute));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timeBuilder.toString());
        return simpleDateFormat.format(new Date(timeMills));
    }

    /**
     * 毫秒转换到日期加时间字符串
     *
     * @param timeMills
     * @return 格式：yyyy-MM-dd HH:mm
     */
    public static String transferToDateTime3(long timeMills){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return simpleDateFormat.format(new Date(timeMills));
    }

    /**
     * 毫秒转换到"周X"
     *
     * @param timeMills
     * @return
     */
    public static String transferToWeekDayName(long timeMills){
        Resources resources = ScheduleApplication.applicationContext.getResources();
        String[] days = new String[]{
                resources.getString(R.string.sunday),
                resources.getString(R.string.monday),
                resources.getString(R.string.tuesday),
                resources.getString(R.string.wednesday),
                resources.getString(R.string.thursday),
                resources.getString(R.string.friday),
                resources.getString(R.string.saturday)
        };
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(timeMills);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        return days[dayOfWeek - 1];
    }

    /**
     * 毫秒转换到日期字符串
     *
     * @param timeMills
     * @return 格式:yyyy-M-d
     */
    public static String transferToDate(long timeMills){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-M-d");
        return simpleDateFormat.format(new Date(timeMills));
    }

    /**
     * 毫秒转换到时间字符串
     *
     * @param timeMills
     * @return
     */
    public static String transferToTime(long timeMills){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(new Date(timeMills));
    }

    /**
     * 返回明天中午的时间
     *
     * @return
     */
    public static long getTomorrowNoonTime(){
        long tomorrow = System.currentTimeMillis() + DAY_MILLIS;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(tomorrow);
        calendar.set(Calendar.HOUR_OF_DAY, 12);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTimeInMillis();
    }

    /**
     * 返回下拉刷新里显示的最后一次更新时间文案
     *
     * @param lastSyncTime
     * @return
     */
    public static String getLastSyncTimeStr(long lastSyncTime){
        String ret = null;
        if(lastSyncTime > 0){
            long now = System.currentTimeMillis();
            long interval = now < lastSyncTime ? 0 : now - lastSyncTime;
            if(interval < HOUR_MILLIS){ // X分钟前更新
                long minute = interval / MINUTE_MILLIS;
                minute = minute == 0 ? 1 : minute;
                ret = minute + ScheduleApplication.applicationContext.getString(R.string.sync_before_minutes);
            } else if(interval < DAY_MILLIS){ // X小时前更新
                long hour = interval / HOUR_MILLIS;
                ret  = hour + ScheduleApplication.applicationContext.getString(R.string.sync_before_houres);
            } else{ // X天前更新
                ret = getDayInterval(now, lastSyncTime) + ScheduleApplication.applicationContext.getString(R.string.sync_before_days);
            }
        }
        return ret;
    }

}
